home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-05-22 | 4.9 KB | 156 lines | [TEXT/CWIE] |
- // =================================================================================
- // PL_Utils.cp 1997 BB's Team inc. All rights reserved
- // =================================================================================
-
- #include "PL_Utils.h"
- #include <UDrawingState.h>
- #include <Sound.h>
-
- unsigned char PL_Utils::sBaseWidthChar = 'W';
- unsigned char PL_Utils::sVersusWidthChar = 'i';
- unsigned char PL_Utils::sWidthRepeat = 6 ;
- unsigned char PL_Utils::sMonoTestSize = 9 ;
-
- int PL_Utils_Init::count=0;
-
- // ====================================================================
- // Initialize PL_Utils from rsrc
- // ====================================================================
- void PL_Utils::Initialize (void)
- {
- Handle h = GetResource ('hide', 128);
- if (h) {
- sBaseWidthChar = *(*h );
- sVersusWidthChar = *(*h+1);
- sWidthRepeat = *(*h+2);
- sMonoTestSize = *(*h+3);
- ReleaseResource (h);
- }
- }
-
-
- // ====================================================================
- // Centers a Rect in another (bigger) one
- // ====================================================================
- void PL_Utils::CenterRect (Rect *toFit, const Rect &frame)
- {
- // pin top-left corner to frame's one
- ::OffsetRect (toFit, frame.left-toFit->left, frame.top-toFit->top);
-
- // add half the difference
- ::OffsetRect (toFit, (frame.right-toFit->right)/2, (frame.bottom-toFit->bottom)/2);
- }
-
-
- // ====================================================================
- // find out the biggest rect proportionnal to toFit that fits in frame
- // ====================================================================
- void PL_Utils::FitRect (Rect *toFit, const Rect &frame)
- {
- float ratioW = (float)(frame.right - frame.left) / (toFit->right - toFit->left);
- float ratioH = (float)(frame.bottom - frame.top ) / (toFit->bottom - toFit->top );
- if (ratioH < ratioW)
- ::SetRect (toFit, toFit->left,
- toFit->top,
- toFit->left + (toFit->right - toFit->left) * ratioH,
- toFit->top + frame.bottom-frame.top);
- else
- ::SetRect (toFit, toFit->left,
- toFit->top,
- toFit->left + frame.right-frame.left,
- toFit->top + (toFit->bottom - toFit->top ) * ratioW);
- }
-
-
- // ====================================================================
- // Counts the bits set in an unsigned char
- // ====================================================================
- int PL_Utils::CountBits (unsigned char u)
- {
- int count = 0;
- while (u) {
- count++;
- u &= u-1;
- }
- return count;
- }
-
-
- // ====================================================================
- // Play a named resource sound (system or in an open rsrc)
- // ====================================================================
- void PL_Utils::PlayNamedSound (Str255 name)
- {
- Handle mySndHandle;
- mySndHandle = GetNamedResource ('snd ', name);
- if ( mySndHandle ) {
- SndPlay (nil, (SndListHandle) mySndHandle, true);
- ReleaseResource (mySndHandle);
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // Compute a font's characteristics
- // ---------------------------------------------------------------------------------
- void PL_Utils::ComputeBBox (
- TextTraitsRecord & inTextTraits,
- Int32 inSize,
- Int32 &outWidth,
- Int32 &outHeight,
- Int32 &outAscent )
- {
- // auto-saves present font characteristics
- StTextState localTextState;
- UTextTraits::SetPortTextTraits (&inTextTraits);
- ::TextSize(inSize);
-
- outWidth = ::CharWidth (sBaseWidthChar);
-
- FontInfo fInfo;
- ::GetFontInfo (&fInfo);
- outAscent = fInfo.ascent;
- outHeight = fInfo.ascent + fInfo.descent;
- }
-
-
- // ---------------------------------------------------------------------------------
- // Returns the width of several times the same char in the current font/size
- // ---------------------------------------------------------------------------------
- Int16 PL_Utils::RepeatCharWidth (unsigned char inChar)
- {
- static unsigned char buffer[255];
- if (!inChar)
- inChar = sBaseWidthChar;
- for (int i=0 ; i<sWidthRepeat ; i++)
- buffer[i]=inChar;
- return ::TextWidth (buffer, 0, sWidthRepeat);
- }
-
- // ---------------------------------------------------------------------------------
- // Guess if a font has a fixed width by comparing two char's repeated widths
- // ---------------------------------------------------------------------------------
- Boolean PL_Utils::IsMonoSpace (Str255 fontName)
- {
- Int16 fontNumber;
- StTextState saveState;
-
- ::GetFNum (fontName, &fontNumber);
- ::TextFont (fontNumber);
- ::TextSize (sMonoTestSize);
-
- return RepeatCharWidth (sBaseWidthChar) == RepeatCharWidth (sVersusWidthChar);
- }
-
-
- // ---------------------------------------------------------------------------------
- // Allocate a Handle in core or temp memory
- // ---------------------------------------------------------------------------------
- Handle PL_Utils::ForceNewHandle (Handle &h, Int32 size)
- {
- h = ::NewHandle (size);
- if (h == nil)
- h = ::NewHandleSys (size);
- return h;
- }
-